/*
 * @Author: yangluming
 * @Date: 2024-12-13 13:44:46
 * @LastEditors: yangluming
 * @LastEditTime: 2024-12-16 17:28:16
 * @Description: 
 */
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:squat_easy/generated/assets.dart';
import 'package:squat_easy/se_main/se_home/recent_run/se_recent_run_logic.dart';
import 'package:squat_easy/se_main/se_home/se_home_page.dart';
import 'package:squat_easy/se_route.dart';
import 'package:ylm_ui_kit/ylm_ui_kit.dart';

class SeRecentRunPage extends GetView<SeRecentRunLogic> {
  const SeRecentRunPage({super.key});

  @override
  Widget build(BuildContext context) {
    return GetBuilder<SeRecentRunLogic>(
      init: SeRecentRunLogic(),
      builder: (logic) {
        return BaseScaffold(
          hideBackBtn: true,
          titleSpacing: 0,
          appbarBackgroundColor: Colors.red,
          child: Stack(
            fit: StackFit.expand,
            children: [
              _buildBg().positioned(left: 0, right: 0, top: 0, bottom: 0),
              _buildTopLogo().positioned(top: ScreenUtil().statusBarHeight + 7.w, left: 0, right: 0),
              _buildBottomBg().positioned(left: 0, right: 0, bottom: 0, height: 86.w),
              _buildWhiteBg().positioned(left: 0, right: 0, top: 140.w + ScreenUtil().statusBarHeight, bottom: 44.w),
              _buildContent().positioned(left: 0, right: 0, top: 160.w + ScreenUtil().statusBarHeight, bottom: 44.w),
              _buildBack().positioned(left: 30, top: ScreenUtil().statusBarHeight + 4.w),
            ],
          ),
        );
      },
    );
  }

  Widget _buildBg() => const ImageWidget.asset(Assets.pngHomeBg, fit: BoxFit.fill);

  Widget _buildTopLogo() => ImageWidget.asset(Assets.pngRecentlyTopLogo, height: 238.w);

  Widget _buildWhiteBg() => const ImageWidget.asset(Assets.pngHomeWhiteBg, fit: BoxFit.fill);

  Widget _buildBottomBg() => const ImageWidget.asset(Assets.pngHomeBottom, fit: BoxFit.fill);

  Widget _buildContent() {
    return Column(
      children: [
        _buildTitle().paddingTop(20.w),
        Expanded(child: controller.list.isEmpty ? _buildEmpty() : _buildList()),
      ],
    );
  }

  Widget _buildList() {
    return ListView.separated(
      padding: EdgeInsets.symmetric(horizontal: 20.w, vertical: 20.w),
      itemCount: controller.list.length,
      itemBuilder: (context, index) {
        return RecentlyRunItem(
            training: controller.list[index],
            titleFontSize: 20,
            dateFormat: 'd MMMM yyyy',
            gradient: const LinearGradient(
              colors: [Color(0XFFE3F6F2), Color(0XFFFEFAF1)],
              begin: Alignment(0.62, 0.98),
              end: Alignment(0.6, 0.32),
            ),
            right: ImageWidget.asset(Assets.pngHomeTimeBlack, width: 18.w));
      },
      separatorBuilder: (BuildContext context, int index) => SizedBox(height: 15.w),
    );
  }

  Widget _buildEmpty() => ImageWidget.asset(Assets.pngHomeEmpty, width: 145.w);

  Widget _buildBack() => ButtonWidget.icon(
        Assets.svgAppBarBack.toSvg(width: 40),
        onTap: SeRoute.close,
        hasInkWell: false,
      );

  Widget _buildTitle() => const TextWidget.oneLine(text: 'Recently run', fontSize: 26, color: Colors.black);
}
